package com.indigon.parkStory.view.objects
{
	import com.indigon.parkStory.data.objects.LillyData;
	import com.indigon.parkStory.model.objects.LillyModel;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;

	public class LillyView extends ObjectView
	{
		private static const LAND_COUNTER        : uint   = 5;
		private static const SINGLE_COUNTER      : uint   = 10;
		private static const SINGLE_SCALE_CHANGE : Number = 0.9;
		private static const SINGLE_ALPHA_CHANGE : Number = 0.0;
		private static const LAND_SCALE_CHANGE   : Number = 0.8;
		private static const FADER_SCALE_CHANGE  : Number = 0.015;	
		
		private static const COLLECT_LABEL : String = "collect";
		
		private var _content     : MovieClip;

		private var _scaleX      : Number;
		private var _scaleY      : Number;
		
		private var _vine 		 : Sprite;
		private var _vineWidth	 : Number;
		
		private var _animationCounter : uint;
		
		public function LillyView(lilly:LillyModel)
		{
			var gfx : MovieClip;
			
			switch (lilly.data.type)
			{
				case LillyData.TYPE_FADE	: gfx = new GFX_FadeLilly();	break;
				case LillyData.TYPE_STATIC	: gfx = new GFX_Rock();			break;
				case LillyData.TYPE_SINGLE	: gfx = new GFX_SingleLilly();	break;
				case LillyData.TYPE_BOUNDED	: gfx = new GFX_BoundLilly();	break;
				case LillyData.TYPE_CARRIER	: gfx = new GFX_LillyFrog();	break;
				case LillyData.TYPE_TURTLE	: gfx = new GFX_Turtle();		break;
				case LillyData.TYPE_NORMAL	: gfx = new GFX_Lilly();		break;
			}
		
			if ((lilly.data.type == LillyData.TYPE_TURTLE) || (lilly.data.type == LillyData.TYPE_CARRIER))
			{
				gfx.stop();
				
			}
			else
			{
				gfx.gotoAndStop(uint(Math.random() * gfx.totalFrames) + 1);
			}
			
			super(gfx, lilly, lilly.data.type != LillyData.TYPE_CARRIER);
			
			graphic.width = graphic.height = lilly.data.radius * 2;
			_scaleX = graphic.scaleX;
			_scaleY = graphic.scaleY;
			
			if (lilly.data.hasStar)
			{
				_content = new GFX_Star();
			}
			else if (lilly.data.hasGoal)
			{
				_content = new GFX_Goal();
				_content.rotation = Math.random() * 360;
				
				var colPos : Point = Point.polar(lilly.data.radius - 8, Math.PI * 2 * Math.random());
				_content.x = colPos.x;
				_content.y = colPos.y;
				
				_content.scaleX = 1 / _scaleX;
				_content.scaleY = 1 / _scaleY;
			}
			
			if (_content != null)
			{
				graphic.addChild(_content);
			}
		}
		
		public override function uninit():void
		{
			if (_content != null)
			{
				_content.parent.removeChild(_content);
				_content = null;
			}
			
			if (_vine != null)
			{
				_vine.parent.removeChild(_vine);
				_vine = null;
			
			}
			super.uninit();
		}
		
		public override function set(container:DisplayObjectContainer, x:Number, y:Number, rotation:Number=0, layer:uint=0):void
		{
			var lilly : LillyModel = LillyModel(_object);
			
			if (lilly.data.isBounded)
			{
				_vine = new GFX_BoundVine();
				_vine.x = x;
				_vine.y = y;
				_vineWidth = _vine.width;
				_vine.scaleX = 0;
				container.addChild(_vine);
			}
			
			var angle : Number;
			
			if (lilly.data.isCarrier)
			{
				angle = lilly.data.carrierAng * 180 / Math.PI + 90;
			}
			else if (lilly.data.isStatic)
			{
				angle = 0;
			}
			else
			{
				(Math.random() * 360);
			}
			
			super.set(container, x, y, angle , container.numChildren);
			
		}
		
		public override function update():void
		{
			var lilly : LillyModel = LillyModel(_object);
			
			if (lilly.data.isSingle && lilly.isDead)
			{
				graphic.alpha -= (1 - SINGLE_ALPHA_CHANGE) / SINGLE_COUNTER;
				graphic.scaleX -= _scaleX * (1 - SINGLE_SCALE_CHANGE) / SINGLE_COUNTER;
				graphic.scaleY -= _scaleY * (1 - SINGLE_SCALE_CHANGE) / SINGLE_COUNTER;
				_animationCounter--;
				if (_animationCounter == 0)
				{
					uninit();
				}
			}
			else
			{
				if (lilly.hasFrog && lilly.data.isFading)
				{
					graphic.alpha = lilly.fadeValue;
					graphic.scaleX = _scaleX - (FADER_SCALE_CHANGE * (1 - lilly.fadeValue));
					graphic.scaleY = _scaleY - (FADER_SCALE_CHANGE * (1 - lilly.fadeValue));		
				}
				
				if (_animationCounter > 0)
				{
					graphic.scaleX += _scaleX * (1 - LAND_SCALE_CHANGE) / LAND_COUNTER;
					graphic.scaleY += _scaleY * (1 - LAND_SCALE_CHANGE) / LAND_COUNTER;
					_animationCounter--;
				}
				
				if (lilly.data.isBounded)
				{
					_vine.scaleX   = -lilly.boundVector.length / _vineWidth;
					_vine.rotation = Math.atan2(lilly.boundVector.y, lilly.boundVector.x) * 180 / Math.PI;
				}
			
				super.update();
			}
		}
		
		public function collectStar():void
		{
			_content.addFrameScript(_content.totalFrames - 1, removeContent);
			_content.gotoAndPlay("collect");
		}
		
		public function collectGoal():void
		{
			_content.addFrameScript(_content.totalFrames - 1, removeContent);
			_content.gotoAndPlay("collect");
		}
		
		private function removeContent():void
		{
			if (!removed)
			{
				_content.stop();
				_content.parent.removeChild(_content);
				_content = null;
			}
		}
		
		public function jumpOn():void
		{
			var lilly : LillyModel = LillyModel(_object);
			
			if ((lilly.data.isTurtle || lilly.data.isCarrier) && (graphic.currentFrameLabel == "idle"))
			{
				graphic.gotoAndPlay("run");
			}
			
			if (!lilly.data.isStatic)
			{
				graphic.scaleX *= LAND_SCALE_CHANGE;
				graphic.scaleY *= LAND_SCALE_CHANGE;
				_animationCounter = LAND_COUNTER;
			}
		}
		
		public function jumpOff():void
		{
			if (!removed)
			{
				var lilly : LillyModel = LillyModel(_object);
				
				if (lilly.data.isFading)
				{
					graphic.alpha = 1;
					graphic.scaleX = _scaleX;
					graphic.scaleY = _scaleY;
				}
				else if (lilly.data.isCarrier)
				{
					graphic.gotoAndStop(1);
				}
			}
		}
		
		public function fadeDrown():void
		{
			uninit();
		}
		
		public function singleDrown():void
		{
			_animationCounter = SINGLE_COUNTER;
		}
	}
}